package com.youkeyi.ddy.cloud.basic.hr.employee.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.hr.employee.entity.TblEmployeeTrainingRecord;
import com.youkeyi.ddy.cloud.basic.hr.employee.service.TblEmployeeTrainingRecordService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.HrEmployeeTrainingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.dto.HrEmployeeTrainingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.dto.HrEmployeeTrainingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 员工培训记录表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-28
 */
@Service
public class TblEmployeeTrainingRecordController implements HrEmployeeTrainingCloudService {

    @Autowired
    private TblEmployeeTrainingRecordService tblEmployeeTrainingRecordService;

    @Override
    public HrEmployeeTrainingListCloudDto query(HrEmployeeTrainingQueryCloudParam param) {
        HrEmployeeTrainingListCloudDto dto = new HrEmployeeTrainingListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getEmployeeTrainingPrimaryKeys())) {
            param.getEmployeeTrainingPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys())) {
            param.getEmployeePrimaryKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblEmployeeTrainingRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getEmployeeTrainingPrimaryKeys()), TblEmployeeTrainingRecord::getPrimaryKey, param.getEmployeeTrainingPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys()), TblEmployeeTrainingRecord::getEmployeeKey, param.getEmployeePrimaryKeys())
                .ne(TblEmployeeTrainingRecord::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblEmployeeTrainingRecord> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblEmployeeTrainingRecord> employeeList = tblEmployeeTrainingRecordService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(employeeList.getRecords())) {
            dto.setTotal(employeeList.getTotal());

            List<HrEmployeeTrainingListItemCloudDto> items = new ArrayList<>();
            employeeList.getRecords().forEach(employee -> {
                HrEmployeeTrainingListItemCloudDto item = new HrEmployeeTrainingListItemCloudDto();
                item.setEmployeeTrainingPrimaryKey(employee.getPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setStartDate(employee.getStartDate() != null ? employee.getStartDate().getTime() : null);
                item.setEndDate(employee.getEndDate() != null ? employee.getEndDate().getTime() : null);
                item.setTrainingName(employee.getTrainingName());
                item.setTrainingOrganization(employee.getTrainingOrganization());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime() != null ? employee.getCreateTime().getTime() : null);
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime() != null ? employee.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(HrEmployeeTrainingCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblEmployeeTrainingRecord employee = new TblEmployeeTrainingRecord();
        employee.setPrimaryKey(SimpleUUidUtils.getUuid());
        employee.setEmployeeKey(param.getEmployeeKey());
        employee.setStartDate(param.getStartDate() != null ? new Date(param.getStartDate()) : null);
        employee.setEndDate(param.getEndDate() != null ? new Date(param.getEndDate()) : null);
        employee.setTrainingName(param.getTrainingName());
        employee.setTrainingOrganization(param.getTrainingOrganization());
        employee.setRemark(param.getRemark());
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        employee.setCreateUserKey(param.getCreateUserKey());
        employee.setCreateUserName(param.getCreateUserName());
        employee.setCreateTime(new Date());
        boolean save = tblEmployeeTrainingRecordService.save(employee);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(employee.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(HrEmployeeTrainingUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeTrainingPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblEmployeeTrainingRecord> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblEmployeeTrainingRecord::getPrimaryKey, param.getEmployeeTrainingPrimaryKey());
        TblEmployeeTrainingRecord employee = new TblEmployeeTrainingRecord();
        employee.setPrimaryKey(param.getEmployeeTrainingPrimaryKey());
        employee.setStartDate(param.getStartDate() != null ? new Date(param.getStartDate()) : null);
        employee.setEndDate(param.getEndDate() != null ? new Date(param.getEndDate()) : null);
        employee.setTrainingName(param.getTrainingName());
        employee.setTrainingOrganization(param.getTrainingOrganization());
        employee.setRemark(param.getRemark());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeTrainingRecordService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(HrEmployeeTrainingDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeTrainingPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblEmployeeTrainingRecord> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblEmployeeTrainingRecord::getPrimaryKey, param.getEmployeeTrainingPrimaryKeys());

        TblEmployeeTrainingRecord employee = new TblEmployeeTrainingRecord();
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeTrainingRecordService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
